import { RuleResponse, StringOrNull } from "@/Types/Types";
import { Validator } from "./Validator";

/**
 * 验证字符串长度不小于指定值
 *
 * @param minLength 指定的最小长度
 * @param err 验证失败时输出的字符串
 *
 * @exmaple
 * MinLengthValidator(10) // 验证错误时会输出 `输入长度必须不小于10`
 */
export const MinLengthValidator = function(
  minLength: number,
  err = `输入长度必须不小于${minLength}`
): Validator<StringOrNull> {
  return function(input: StringOrNull): RuleResponse {
    if (input != null && input.length < minLength) {
      return err;
    }
    return true;
  };
};
